<?php

namespace App\Console\Commands\Fy;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Console\Command;

class FyTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fy:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '测试富友进件编码问题';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 富友文档：https://fundwx.fuiou.com/doc-yzf/#/scanentry/introduction?id=%e9%80%9a%e8%ae%af%e6%96%b9%e5%bc%8f
     */
    public function handle()
    {
        $data = [
            "trace_no"              => "1695452786001",
            "ins_cd"                => "08A9999999",
            "mchnt_name"            => "崂山区牛焗匠炸串店",
            "mchnt_shortname"       => "青岛市3957夸父炸串店",
            "real_name"             => "崂山区牛焗匠炸串店",
            "license_type"          => "B",
            "license_no"            => "92370212MAC4XCCE6C",
            "license_expire_dt"     => 20991231,
            "certif_id"             => "372925199711087513",
            "certif_id_expire_dt"   => "20281025",
            "contact_person"        => "李响",
            "contact_phone"         => "13162514517",
            "contact_addr"          => "山东省青岛市崂山区香港东路195号乙金狮广场全食区B1-19",
            "contact_mobile"        => "13162514517",
            "contact_email"         => "lixiang@kuafu.tech",
            "business"              => "557",
            "city_cd"               => "4520",
            "county_cd"             => "4525",
            "acnt_type"             => 2,
            "bank_type"             => "0402",
            "inter_bank_no"         => "402475502599",
            "iss_bank_nm"           => "山东单县农村商业银行李新庄支行",
            "acnt_nm"               => "牛群",
            "acnt_no"               => "6223203445589169",
            "settle_tp"             => "1",
            "artif_nm"              => "牛群",
            "acnt_artif_flag"       => 1,
            "acnt_certif_tp"        => 0,
            "acnt_certif_expire_dt" => 20991231,
            "acnt_certif_id"        => "372925199711087513",
            "card_start_dt"         => "20181025",
            "contact_cert_no"       => "372925199711087513",
            "lic_regis_addr"        => "山东省青岛市崂山区香港东路195号乙金狮广场全食区B1-19",
            "license_start_dt"      => "20221227",
            "wx_busi_flag"          => "0",
            "wx_flag"               => "1",
            "wx_set_cd"             => "M0000",
            "ali_flag"              => "1",
            "ali_set_cd"            => "M0000",
            "acnt_contact_mobile"   => "15864445677",
            "has_reserve_acnt"      => 0,
            "reserve_acnt_no"       => "",
            "reserve_acnt_nm"       => "",
            "reserve_inter_bank_no" => "",
            "reserve_iss_bank_nm"   => "",
            "reserve_bank_type"     => "",
        ];
        $url = 'http://www-1.fuiou.com:28090/wmp/wxMchntMng.fuiou?action=wxMchntAdd';
        self::postDataXml($url, $data);
    }

    public function postDataXml($url, $postData, $dataCode = 'GBK')
    {
        $strGBK           = mb_convert_encoding($postData, 'GBK', 'UTF-8');
        $postData         = $strGBK;
        $postData['sign'] = self::hmacSign($postData);
        $postDataXml      = self::toXml($postData);
//        dd($postDataXml);
        $response = (new Client(
            [
                RequestOptions::TIMEOUT => 10,
                RequestOptions::VERIFY  => false
            ]
        ))->post($url, ['form_params' => ['req' => urlencode($postDataXml)]]);

        $result      = urldecode($response->getBody()->getContents());
        $strUTF8     = mb_convert_encoding($result, 'utf-8', 'gb2312');
        $responseStr = str_replace($dataCode, 'utf-8', $strUTF8);
        $responseStr = simplexml_load_string($responseStr);
        $result      = json_decode(json_encode($responseStr), true);
        dd($result);

    }

    /**
     * 密钥
     * @param $params
     * @return string
     */
    public function hmacSign($params): string
    {
        ksort($params);
        $params['key'] = '040f23510fbf4b34ae3895272e83c58c';
        $paramsString  = "";
        foreach ($params as $k => $v) {
            if ($v !== "") {
                $paramsString .= $k . '=' . $v . '&';
            }
        }
        $paramsString = substr($paramsString, 0, -1);

        return md5($paramsString);
    }


    /**
     * XML编码
     * @param mixed $data 数据
     * @param string $encoding 数据编码
     * @param string $root 根节点名
     * @return string
     */
    public function toXml($data, $encoding = 'GBK', $root = 'xml'): string
    {
        $xml = '<?xml version="1.0" encoding="' . $encoding . '" standalone="yes"?>';
        $xml .= '<' . $root . '>';
        $xml .= self::data2xml($data);
        $xml .= '</' . $root . '>';
        return $xml;
    }


    /**
     * 数据XML编码
     * @param mixed $data 数据
     * @return string
     */
    public function data2xml($data): string
    {
        $xml = '';
        foreach ($data as $key => $val) {
            is_numeric($key) && $key = "item id=\"$key\"";
            $xml .= "<$key>";
            $xml .= (is_array($val) || is_object($val)) ? self::data2xml($val) : $val;
            list($key,) = explode(' ', $key);
            $xml .= "</$key>";
        }
        return $xml;
    }
}
